package io.renren.modules.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.modules.business.dao.BusAttendrecordDao;
import io.renren.modules.business.entity.BusAttendrecordEntity;
import io.renren.modules.business.service.BusAttendrecordService;
import io.renren.modules.business.util.PageLastUtil;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("busAttendrecordService")
public class BusAttendrecordServiceImpl extends ServiceImpl<BusAttendrecordDao, BusAttendrecordEntity> implements BusAttendrecordService {

    private static String SIGNIN_SUCCESS = "签到成功";
    private static String SIGNIN_FALSE = "签到失败";
    private static String SIGNBACK_SUCCESS = "签退成功";
    private static String SIGNBACK_FALSE = "签退失败";

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String userName = (String) params.get("userName");
        List<Map<String, Object>> list;
        if (!"".equals(userName) && userName.length()!=0) {
            //关键字查询
            list = baseMapper.attendanceByKey(userName);
        }else {
            //查询列表
            list = baseMapper.attendanceList();
        }
        return PageLastUtil.getPageUtils(params, list);
    }

    @Override
    public String attendance(Map<String, Object> params) {
        String phone = (String) params.get("mobile");
        Long labId = baseMapper.deveLab(phone);
        Date now = new Date();
        String nowTime = dateToString(now);
        //获取考勤用户最早的考勤记录信息
        BusAttendrecordEntity attendrecord = baseMapper.attendByPhone(phone);
        //判断用户最早的考勤类型是否为签到
        if (attendrecord.getType() == 0) {
            //未签退
            Date time = timeFormat(attendrecord.getTime());
            //判断是否大于18小时
            if (isOverTime(now, time)) {
                //先签退，后签到
                Date signBackTime = singBackTime(now);
                boolean b = baseMapper.attendance(phone, 1, labId, dateToString(signBackTime));
                if (b) {
                    return signIn(phone, labId, nowTime);
                }else {
                    return SIGNIN_FALSE;
                }
            }else {
                //签退
                return singBack(phone, labId, nowTime);
            }
        }else {
            //签到
            return signIn(phone, labId, nowTime);
        }
    }

    /**
     * 签到
     * @param phone
     * @param labId
     * @param nowTime
     * @return
     */
    public String signIn(String phone, Long labId, String nowTime) {
        boolean b = baseMapper.attendance(phone, 0, labId, nowTime);
        if (b) {
            return SIGNIN_SUCCESS;
        }else {
            return SIGNIN_FALSE;
        }
    }

    /**
     * 签退
     * @param phone
     * @param labId
     * @param nowTime
     * @return
     */
    public String singBack(String phone, Long labId, String nowTime) {
        boolean b = baseMapper.attendance(phone, 1, labId, nowTime);
        if (b) {
            return SIGNBACK_SUCCESS;
        }else {
            return SIGNBACK_FALSE;
        }
    }

    /**
     * 将时区格式的时间转换为Date形式
     * @param date
     * @return
     */
    public Date timeFormat(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将Date转换为String
     * @param date
     * @return
     */
    public String dateToString(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        return format.format(date);
    }

    /**
     * 为当前时间添加18小时
     * @param date
     * @return
     */
    public Date singBackTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, 18);
        return calendar.getTime();
    }

    /**
     * 判断最新考勤记录距当前时间是否超过18小时
     * @param time
     * @return
     */
    public boolean isOverTime(Date now, Date time) {
        //计算时长
        int day = (int) ((now.getTime() - time.getTime()) / (1000 * 60 * 60 * 24));
        int hour = (int) ((now.getTime() - time.getTime()) / (1000 * 60 * 60));
        if (day == 0 && hour <= 18) {
            return false;
        }else {
            return true;
        }
    }
}
